import math,os
from flask import Blueprint,request,jsonify
from application.models.user import *

user_bp = Blueprint("user_bp",__name__)

@user_bp.route('/getuser')
def getuser():
    data1 = User.request.all()
    list1 = []
    for i in data1:
        list1.append({
        'uid':i.uid,
        'username':i.username,
        'password':i.password,
    })
    return jsonify({'code':200,'msg':'获取用户信息成功','data1':list1})

@user_bp.route('/GetPicture')
def GetPicture():
    data2 = Picture.request.all()
    list2 = []
    for i in data2:
        list2.append({
            'pid':i.pid,
            'pname':i.pname,
            'types':i.types,
        })
    return jsonify({'code':200,'msg':'展示所有相册类型成功','data2':list2})

@user_bp.route('/tupian')
def tupian():
    data3 = Tupian.request.all()
    list3 = []
    for i in data3:
        list3.append({
            'id':i.pid,
            'leixing':i.leixing
        })
    return jsonify({'code':200,'msg':'展示所有图片信息成功','data3':list3})

@user_bp.route("/delindex/<int:id>",methods=['DELETE'])
def delindex(id):
    Picture.query.filter_by(id=id).delete()
    db.session.commit()
    return jsonify({"code":200,'msg':'删除成功'})


@user_bp.route("/upload",methods=['POST'])
def upload():
    file = request.files.get("file")
    path = os.path.join("upload"+file.filename)
    file.save(path)
    return jsonify({'code':200,"url":path})



